package com.example.bookinfo; // 替换为你的包名

import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import android.content.Intent;
import android.os.Bundle;

import java.util.List;

public class MainActivity extends AppCompatActivity {
    private RecyclerView recyclerView;
    private BookAdapter adapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        // 初始化RecyclerView
        recyclerView = findViewById(R.id.recyclerView);
        recyclerView.setLayoutManager(new LinearLayoutManager(this));

        // 获取图书数据
        List<Book> books = BookDataSource.getBooks();

        // 创建并设置适配器
        adapter = new BookAdapter(books, this);

        // 设置点击监听器
        adapter.setOnItemClickListener(new BookAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(Book book) {
                // 创建跳转到详情页的Intent
                Intent intent = new Intent(MainActivity.this, BookDetailActivity.class);

                // 传递图书数据
                intent.putExtra("title", book.getTitle());
                intent.putExtra("author", book.getAuthor());
                intent.putExtra("description", book.getDescription());

                // 启动详情页
                startActivity(intent);

                // 添加页面切换动画（淡入淡出效果）
                overridePendingTransition(android.R.anim.fade_in, android.R.anim.fade_out);
            }
        });

        // 设置适配器
        recyclerView.setAdapter(adapter);
    }

    @Override
    public void onBackPressed() {
        super.onBackPressed();
        // 返回时也添加动画效果
        overridePendingTransition(android.R.anim.fade_in, android.R.anim.fade_out);
    }
}